import { Config } from "../../Config"
import { BigEvolution, BigState } from "../core/Big"
import { Player } from "../core/Player"
import { Terrain } from "../core/Point"
import GameUtils from "../GameUtils"
import { BaseHandler } from "./BaseHandler"

export class 沙皇Handler extends BaseHandler{

    move(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_BIG_MOVE)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        let big_end = endPoint.big
        if(big_end != null) {
            big_end.deleteState(BigState.HIDE)
            big_start.die(player,endPoint.id)
            return
        }
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        big_start.terrainAdapt()
        if (big_start.is_death) {
            return
        }
        this.gameMgr.sendBroadcast(GameUtils.getObj("big_move",{"start":data.start,"end":data.end}))
    }

    /** 大子攻击 */
    attack(data:any,player:Player) {
        this.handlerMgr.spendAction(player,Config.COST_BIG_ATTACK)
        let startPoint = this.gameMgr.map_point.get(data.start)
        let endPoint = this.gameMgr.map_point.get(data.end)
        let big_start = startPoint.big
        big_start.actionOnce()
        let small = endPoint.small
        let big_end = endPoint.big
        if(small != null) {
            //小子被攻击
            small.die(player)
        } else if(big_end != null) {
            //大子被攻击
            if(big_end.attacked(player)){
               //打过了
            } else {
                //攻击没打过，自己死亡
                big_start.die(player,endPoint.id)
                return
            }
        }
        //没有大子，就是移动
        startPoint.big = null
        endPoint.big = big_start
        big_start.point = endPoint
        big_start.terrainAdapt()
        if (big_start.is_death) {
            return
        }
        this.gameMgr.sendBroadcast(GameUtils.getObj("big_attack",{"start":data.start,"end":data.end}))
    }
}